/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.MessageTags;

/**
 * An instance of MessageTagsDAO is a data access object used to access
 * MessageTags, which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class MessageTagsDAO extends BaseDAO {

	public void deleteByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "delete   from  MessageTags  where "
				+ propertyName + "= ? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		queryObject.executeUpdate();
	}

	public List<MessageTags> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from MessageTags";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<MessageTags> results = queryObject.list();

		return results;

	}

	public MessageTags findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		MessageTags instance = (MessageTags) session.get(
				MessageTags.class.getName(), id);

		return instance;

	}

	public List<MessageTags> findByMidAndTagid(java.lang.Integer mid,
			Integer tagid) {
		Session session = HibernateSessionFactory.getSession();

		String queryString = "from MessageTags where mid=? and tagid=?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, mid);
		queryObject.setParameter(1, tagid);
		@SuppressWarnings("unchecked")
		List<MessageTags> results = queryObject.list();

		return results;

	}

	public List<MessageTags> findByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from MessageTags as model where model."
				+ propertyName + "= ? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		@SuppressWarnings("unchecked")
		List<MessageTags> results = queryObject.list();

		return results;

	}

	public List<MessageTags> findByProperty(String propertyName, Object value,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from MessageTags as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);

		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<MessageTags> results = queryObject.list();

		return results;

	}

	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from MessageTags as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

}